package com.csyf.manager.service.impl;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.dao.SysMenuMapper;
import com.csyf.manager.entity.SysMenu;
import com.csyf.manager.service.SysMenuService;
import com.csyf.manager.utils.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: casper
 * @Description: 菜单Menu
 * @Date: Created in 2018/11/19 11:38
 */
@Slf4j
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 查询所有权限集合
     * @param status
     * @return
     */
    @Override
    public List<SysMenu> selectAll(Integer status) {
        return sysMenuMapper.selectAllMenus(status);
    }

    /**
     * 查询全部菜单
     * @param status
     * @return
     */
    @Override
    public List<SysMenu> selectAllMenuName(Integer status) {
        return sysMenuMapper.selectAllMenuName(status);
    }

    /**
     * 根据用户id查询权限集合
     * @param userId
     * @return
     */
    @Override
    public Set<String> findMenuByUserId(Long userId) {
        return sysMenuMapper.findMenuByUserId(userId);
    }

    /**
     * 根据用户id查询菜单集合
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> selectMenuByUserId(Long userId) {
        return sysMenuMapper.selectMenuByUserId(userId);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @Override
    public int insert(SysMenu menu) {
        Date date = new Date();
        menu.setMenuId(IdGenerator.INSTANCE.nextId());
        menu.setStatus(CommonConst.STATUS_VALID);
        menu.setCreateTime(date);
        menu.setUpdateTime(date);
        return sysMenuMapper.insert(menu);
    }

    /**
     * 根据菜单id查询菜单
     * @param menuId
     * @return
     */
    @Override
    public SysMenu findByMenuId(String menuId) {
        return sysMenuMapper.selectByMenuId(menuId);
    }

    /**
     * 根据主键id查询菜单
     * @param parentId
     * @return
     */
    @Override
    public SysMenu findById(Integer parentId) {
        return sysMenuMapper.selectById(parentId);
    }

    /**
     * 根据菜单id更新菜单
     * @param menu
     * @return
     */
    @Override
    public int updateByMenuId(SysMenu menu) {
        return sysMenuMapper.updateByMenuId(menu);
    }

    /**
     * 查询子菜单条数
     * @param menuId
     * @return
     */
    @Override
    public int selectSubMenuByMenuId(String menuId) {
        return sysMenuMapper.selectSubMenuByMenuId(menuId);
    }

    /**
     * 更新菜单状态
     * @param menuId
     * @param status
     * @return
     */
    @Override
    public int updateStatus(String menuId, Integer status) {
        return sysMenuMapper.updateStatusByMenuId(menuId, status);
    }

    /**
     * 根据角色id查询权限集合
     * @param roleId
     * @return
     */
    @Override
    public List<SysMenu> findMenusByRoleId(String roleId) {
        return sysMenuMapper.findMenusByRoleId(roleId);
    }
}
